/**
 * 
 * 
 */

var statety=[];//选中策略数组

 $(function(){
    	
    	initStrategyType();

    	showUnBundStrategy();
    });
    
 
    
    
    function initStrategyType(){
    	
    	$("#statetyType").html("");
    	if(resourceType == 'os'){
			statetyType = '<label class="checkbox inline">'+
			'<input type="checkbox" id="allStrategyType" name="allStrategyType" onclick="javascript:checkStrategyType(this)" value="allStrategyType">'+
			'全部</label>'+
			'<label class="checkbox inline">'+
			'<input type="checkbox" id="perfStrategy" name="strategyType"  onclick="javascript:manuStrategyTypeCheck(this)" value="perf">'+
			'性能</label>'+
			'<label class="checkbox inline"><input type="checkbox" id="capacStrategy" name="strategyType" onclick="javascript:manuStrategyTypeCheck(this)" value="capac">'+
     		'容量</label>'+
     		'<label class="checkbox inline">'+
     		'<input type="checkbox" id="configStrategy" name="strategyType" onclick="javascript:manuStrategyTypeCheck(this)" value="config">'+
     		'配置</label><label class="checkbox inline">'+
     		'<input type="checkbox" id="serviceStrategy" name="strategyType" onclick="javascript:manuStrategyTypeCheck(this)" value="service">'+
     		'服务</label>'+
     		'<label class="checkbox inline"><input type="checkbox" id="processStrategy" name="strategyType" onclick="javascript:manuStrategyTypeCheck(this)" value="process">'+
     		'进程</label>'+
     		'<label class="checkbox inline"><input type="checkbox" id="stateStrategy" name="strategyType" onclick="javascript:manuStrategyTypeCheck(this)" value="state">'+
     		'状态</label>'+
     		'<label class="checkbox inline"><input type="checkbox" id="stateStrategy" name="strategyType" onclick="javascript:manuStrategyTypeCheck(this)" value="temp">'+
     		'温度</label>';
		}else if(resourceType == 'db'){
			statetyType = '<label class="checkbox inline">'+
            '<input type="checkbox" id="allStrategyType" name="allStrategyType" onclick="javascript:checkStrategyType(this)" value="allStrategyType">'+
            	'全部'+
            '</label>'+
            '<label class="checkbox inline">'+
                '<input type="checkbox" id="perfStrategy" name="strategyType"  onclick="javascript:manuStrategyTypeCheck(this)" value="mysql">'+
                	'MySql'+
            '</label>'+
            '<label class="checkbox inline">'+
            	'<input type="checkbox" id="perfStrategy" name="strategyType"  onclick="javascript:manuStrategyTypeCheck(this)" value="oracle">'+
            		'Oracle'+
        	'</label>';
		}else if(resourceType == 'network'){
			statetyType = '<label class="checkbox inline">'+
            '<input type="checkbox" id="allStrategyType" name="allStrategyType" onclick="javascript:checkStrategyType(this)" value="allStrategyType">'+
            	'全部'+
            '</label>'+
            '<label class="checkbox inline">'+
			'<input type="checkbox" id="perfStrategy" name="strategyType"  onclick="javascript:manuStrategyTypeCheck(this)" value="networkperf">'+
			'性能</label>'+
     		'<label class="checkbox inline">'+
     		'<input type="checkbox" id="configStrategy" name="strategyType" onclick="javascript:manuStrategyTypeCheck(this)" value="networkconfig">'+
     		'配置</label>';
		}
    	$("#statetyType").html(statetyType);
    };
    
    /**策略列表  复选框触发方法
     * @param f
     */
    function    manucheck(f){
        var id=$(f).parent().parent().attr("nodeId");
        if($(f).attr('checked')){
        	statety.push(id);
        }else{
       	 var index = eq.indexOf(id);
       	 if (index > -1) {
       		statety.splice(index, 1);
        }
   	}
        }
  	
    /**复选框全选后操作
     * @param f
     */
    function afterSelectAll(f){	
    	 var divID=$(f).attr("divID");
    	 $("input[type='checkbox'][divID='"+divID+"'][nodeId]").each(function (){
    		 if($(f).attr('checked')){
    			 statety.push($(this).attr("nodeId"));
    	     }else{
    	    	 var index = groupId.indexOf($(this).attr("nodeId"));
    	    	 if (index > -1) {
    	    		 statety.splice(index, 1);
    	          }
    		} 
    	 });   
    }

	
    function showUnBundStrategy(){
    	 var strategyName=$("#strategyName").val();
    	//获取策略类型
    	var strategyType="";
    	$("input:[name='strategyType']:checked").each(function(){
    		strategyType=strategyType+"#"+$(this).val()+"#,";
        });
    	if(!strategyType==''){
    		strategyType=strategyType.substring(0,strategyType.length-1);
    	}
    	
    	
    	var Strategy = [ {
			code : 'sID',
			name : 'checkBox',
			width : 10
		}, {
			code : 'name',
			name : '名称'
		}, {
			code : 'type',
			name : '类型'
		}, ];
		var parm = {
			url : basePath + '/radar/strategy/getStrategyView.do',
			divID :'strategyList',
			afterQuery:'checkStrategy'
			
		};// 常规参数
		var data = {
			"resourceType" :resourceType ,
			"resourceId":resourceId,
			"strategyType":strategyType,
			"strategyName":strategyName
			
		};
		easyQuery(Strategy, parm, data);
    }
    
    
function    checkStrategy(){
	  for (var i = 0; i < statety.length; i++) {
		  $("#"+statety[i]+" input[type='checkbox']").prop("checked", true);
	}	
}
    //保存策略
function saveStrategy(){
    	var strategyIds="";
    	//获取选中的策略
    	for (var i = 0; i < statety.length; i++) {
    		strategyIds=strategyIds+statety[i]+',';
    	}
    	if(strategyIds!=""){
    		strategyIds=strategyIds.substring(0,strategyIds.length-1);
    	}
		//执行存储
    	$.ajax({
    		type:"POST",
    		url:basePath+"/radar/strategy/saveStrategys.do",
    		dataType:'json',
    		data:{
    			"resourceId":resourceId,
    			"strategyIds":strategyIds
    		},
    		success:function(result){
    			var result = result.data;
    			parent.reQuery("strategy");
    			 if(result == "success"){
    				layer.msg('保存成功！', {icon : 1});
    		    
    				closeWindow();
 	            }else if(result =="0"){  
 	            	layer.msg('保存失败！');
 
 	            	closeWindow();
 	            }
    		}
    	});
    	
    }
    
    function closeWindow(){
    	var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }